Skip to main content

Intersection of Three Sorted Arrays

LeetCode Link

Problem Description​

Visit LeetCode for the full problem description.


Solutions​

Solution 1: C# (Best: 136 ms)​

MetricValue
Runtime136 ms
Memory43.2 MB
Date2021-11-12
Solution
public class Solution {
public IList<int> ArraysIntersection(int[] arr1, int[] arr2, int[] arr3) {
Array.Sort(arr1);
Array.Sort(arr2);
Array.Sort(arr3);

int i = 0;
int j = 0;
int k = 0;

HashSet<int> result = new HashSet<int>();
while(i<arr1.Length && j<arr2.Length && k < arr3.Length)
{
int min = Math.Min(arr1[i], Math.Min(arr3[k], arr2[j]));
if(arr1[i] == arr2[j] && arr2[j] == arr3[k])
{
result.Add(arr1[i]);
i++;
j++;
k++;
}
else if(min == arr1[i]){
i++;
}
else if(arr2[j] == min){
j++;
}
else {
k++;
}
}
return result.ToList();
}
}

Complexity Analysis​

ApproachTimeSpace
SolutionTo be analyzedTo be analyzed